package team.you.change.service.serviceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.you.change.dao.TeacherMapper;
import team.you.change.dao.TimeTableMapper;
import team.you.change.util.Result;
import team.you.change.pojo.Teacher;
import team.you.change.pojo.TimeTable;
import team.you.change.service.TeacherService;
import team.you.change.service.TimeTableService;
import team.you.change.util.YuoUtil;

import java.util.List;

@Service
public class OtherServiceImpl implements TeacherService, TimeTableService {
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private TimeTableMapper timeTableMapper;
    @Override
    public Result getAllTeachers(Integer page, Integer limit) {
        Result result = new Result();
        List<Teacher> teachers = teacherMapper.selectAll(YuoUtil.getPage(page, limit), limit);
        Integer count = teacherMapper.getCount();
        return YuoUtil.setData(result, teachers, count);
    }

    @Override
    public Result update(String id, String value, String field) {
        Result result = new Result();
        Teacher teacher = teacherMapper.selectByPrimaryKey(Integer.parseInt(id));
        if(teacher.getTeacherNo().equals(value)){
            result.setCode(500);
            result.setMsg("工号重复");
            return result;
        }else{
            try {
                return YuoUtil.crudMsg(teacherMapper.UpdateByKey(id, value, field), result);
            } catch (Exception e) {
                return YuoUtil.setErrorMsg(e, result);
            }
        }
    }

    @Override
    public Result getAllTimeTable() {
        Result result = new Result();
        List<TimeTable> timeTables = timeTableMapper.selectAll();
        result.setData(timeTables);
        return result;
    }
    @Override
    public Result state(Integer id, Integer value, String field) {
        Result result = new Result();
        try{
            return YuoUtil.crudMsg(teacherMapper.updateStateById(id, value, field), result);
        }catch (Exception e){
            return YuoUtil.setErrorMsg(e, result);
        }
    }

    @Override
    public Result selectByLike(String value, Integer type, Integer page, Integer limit) {
        Result result = new Result();
        List<Teacher> teachers = teacherMapper.selectByLike(value, type, YuoUtil.getPage(page, limit), limit); //模糊查询时分页
        List<Teacher> count = teacherMapper.selectByLike(value, type, null, null);
        return YuoUtil.setData(result, teachers, count.size());
    }
}
